package com.example.usergroup.viewmodel;

import androidx.databinding.ObservableField;

import com.example.mvvmcommon.mvvm.viewmodel.BaseViewModel;
import com.example.networkmoudel.entity.BaseEntity;
import com.example.usergroup.data.entity.UserEventEntity;
import com.example.usergroup.model.RegModel;

import org.greenrobot.eventbus.EventBus;

import java.util.HashMap;
import java.util.Map;
import java.util.Timer;
import java.util.TimerTask;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

public class RehViewModel extends BaseViewModel<RegModel> {
    public ObservableField<String>mobile=new ObservableField<>();
    public ObservableField<String>pwd=new ObservableField<>();
    public ObservableField<String>pwd2=new ObservableField<>();
    public ObservableField<String>verifyCode=new ObservableField<>();
    public ObservableField<String>getTv=new ObservableField<>();
    private Timer timer;
    private int count = 60;
    @Override
    protected void initData() {
        getTv.set("获取验证码");
    }

    @Override
    protected RegModel createModel() {
        return new RegModel();
    }

    @Override
    protected void sucessFull(BaseEntity entity) {
        if (entity.getStatus()==0){
//            ARouter.getInstance().build("/user/login").navigation();
            UserEventEntity userEventEntity = new UserEventEntity();
            userEventEntity.phone=mobile.get();
            userEventEntity.pwd=pwd.get();
            userEventEntity.action = "update";
            EventBus.getDefault().post(userEventEntity);
            getUiChangeLiveData().getFinishActivity().setValue(""+System.currentTimeMillis());
        }else {
            showMsg(entity.getMessage());
        }
    }

    public void getVerifyCode(){
        timer = new Timer();
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                count--;
                getTv.set("倒计时:"+count+"秒");
                showMsg(getTv.get().toString()+"");
                if (count==0){
                    getTv.set("获取验证码");
                    count=60;
                    timer.cancel();
                }
            }
        },0,1000);
        verifyCode.set("123456");
    }
    public void reg(){
        if (!pwd.get().equals(pwd2.get())){
           showMsg("两次密码不一致");
           return;
        }
        Map<String,Object> map=new HashMap<>();
        map.put("mobile",mobile.get());
        map.put("pwd",pwd.get());
        map.put("verifyCode",verifyCode.get());
        m.requestReg(map).observeOn(AndroidSchedulers.mainThread())
                .subscribeOn(Schedulers.io())
                .subscribe(this);
    }

    public void toBack(){
        getUiChangeLiveData().setFinishActivity(""+System.currentTimeMillis());
    }
}
